package MySQL;

import back.Sale;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class MySQL_Sale {
    public boolean addSale(Sale sale) {
        String sql = "INSERT INTO sale (sale_id, name, amount, quantity, time) VALUES (?, ?, ?, ?, ?)";
        try(Connection conn = DatabaseConnection.getConnection();
            PreparedStatement pstmt = conn.prepareStatement(sql)){
            pstmt.setString(1, sale.getId());
            pstmt.setString(2, sale.getName());
            pstmt.setDouble(3,  sale.getAmount());
            pstmt.setInt(4,sale.getQuantity());
            pstmt.setInt(5,sale.getTime());
            int rows = pstmt.executeUpdate();
            return rows > 0;

        }catch(SQLException e){
            e.printStackTrace();
            return false;
        }
    }
    public boolean updateSale(Sale sale) {
        String sql = "UPDATE sale SET quantity = ? WHERE sale_id = ?";
        try(Connection conn = DatabaseConnection.getConnection();
            PreparedStatement pstmt = conn.prepareStatement(sql)){
            pstmt.setInt(1, sale.getQuantity());
            pstmt.setString(2, sale.getId());
            int rows = pstmt.executeUpdate();
            return rows > 0;

        }catch(SQLException e){
            e.printStackTrace();
            return false;
        }
    }
    public boolean deleteSale(Sale sale) {
        String sql = "DELETE FROM sale WHERE sale_id = ?";
        try(Connection conn = DatabaseConnection.getConnection();
            PreparedStatement pstmt = conn.prepareStatement(sql)){
            pstmt.setString(1, sale.getId());
            int rows = pstmt.executeUpdate();
            return rows > 0;

        }catch(SQLException e){
            e.printStackTrace();
            return false;
        }
    }
    public List<Sale> getAllSale(){
        List<Sale> sales = new ArrayList<>();
        String sql = "SELECT * FROM sale";
        try(Connection conn = DatabaseConnection.getConnection();
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql)){

            while(rs.next()){
                Sale sale = new Sale();
                sale.setId(rs.getString("sale_id"));
                sale.setName(rs.getString("name"));
                sale.setAmount(rs.getDouble("amount"));
                sale.setQuantity(rs.getInt("quantity"));
                sale.setTime(rs.getInt("time"));
                sales.add(sale);
            }
        }catch(SQLException e){
            e.printStackTrace();
        }
        return sales;
    }

    public Sale getSaleByIP(String id){
        Sale sale = new Sale();
        String sql = "SELECT * FROM sale WHERE sale_id = ?";
        try(Connection conn = DatabaseConnection.getConnection();
            PreparedStatement pstmt = conn.prepareStatement(sql)){

            pstmt.setString(1, id);
            ResultSet rs = pstmt.executeQuery();
            if(rs.next()){
                sale.setId(rs.getString("product_id"));
                sale.setName(rs.getString("name"));
                sale.setAmount(rs.getDouble("amount"));
                sale.setQuantity(rs.getInt("quantity"));
                sale.setTime(rs.getInt("time"));
                return sale;
            }else {
                System.out.println("没有" + id);
                return null;
            }

        }catch(SQLException e){
            e.printStackTrace();
            return null;
        }
    }
}
